﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Model = Argos.Data.Model;
using Argos.Data.SqlServer;
using System.Transactions;
using Argos.Workflows;
using Argos.Activities.Tests;

namespace Argos.Core.Tests
{
	/// <summary>
	/// Summary description for SQLTests
	/// </summary>
	[TestClass]
	public class SQLTests
	{

		#region public TestContext TestContext { get; set; }

		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		#endregion



		#region Test: public void Connect01()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void Connect01()
		{
			ArgosDatabase db = new ArgosDatabase();
			Assert.IsTrue(db.DatabaseExists());
		}

		#endregion
		


		#region Test: public void AddTarget01()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void AddTarget01()
		{
			using (TransactionScope scope = new TransactionScope())
			{
				Test test = new Test();
				test.TestId = Guid.NewGuid();
				test.TestActivityType = typeof(WebPageContentTestActivity).FullName;
				test.TestExecutionTimeout = 1000;
				
				Target target = new Target();
				target.TargetId = Guid.NewGuid();
				target.Enabled = true;
				target.MonitoringWorkflowType = typeof(TargetFailureMonitoringWorkflow).FullName;
				target.Name = "Le Target";
				target.TestExecutionInterval = 60;
				target.TestId = test.TestId;
				
				ArgosDatabase db = new ArgosDatabase();
				int count = db.Targets.Count();
				db.Tests.InsertOnSubmit(test);
				db.Targets.InsertOnSubmit(target);
				db.SubmitChanges();

				ArgosDatabase db2 = new ArgosDatabase();
				Assert.AreEqual<int>(count + 1, db2.Targets.Count());
			}
		}

		#endregion

		#region Test: public void AddTarget02()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void AddTarget02()
		{
			Model.Target target = new Argos.Data.Model.Target();
			target.Description = "Le Description";
			target.Enabled = true;
			target.MonitoringWorkflowType = typeof(TargetFailureMonitoringWorkflow);
			target.MonitoringWorkflowProperties["FailureAlertThreshold"] = 3;
			target.MonitoringWorkflowProperties["AlertForChangedTestError"] = true;
			target.Name = "My Target";
			target.TestExecutionInterval = TimeSpan.FromSeconds(24);

			target.Test.ActivityType = typeof(WebPageContentTestActivity);
			target.Test.ActivityProperties["Url"] = new Uri("http://www.codeplex.com/Argos");
			target.Test.ActivityProperties["FollowRedirection"] = false;
			target.Test.ExecutionTimeout = TimeSpan.FromMilliseconds(950);

			Target dbTarget = null;
			using (TransactionScope scope = new TransactionScope())
			{
				ArgosDatabase db = new ArgosDatabase();
				target.InsertOnSubmit(db);
				db.SubmitChanges();

				var targets = from t in db.Targets where t.TargetId == target.Id select t;
				dbTarget = targets.FirstOrDefault();

				Assert.IsNotNull(dbTarget);
				Assert.AreEqual<Guid>(target.Id, dbTarget.TargetId);
				Assert.AreEqual<Guid>(target.Test.Id, dbTarget.Test.TestId);
			}

		}

		#endregion
		
	}
}
